package com.android.vgongyi.service;

import java.util.ArrayList;

import android.app.IntentService;
import android.content.Intent;
import android.util.Log;

import com.android.vgongyi.dao.CommonDAO;
import com.android.vgongyi.entity.CommonEntity;

public class VGongyiUpdateService extends IntentService{
	private static String TAG= "VGongyiUpdateService";

	public VGongyiUpdateService() {
		super(TAG);
	}

	@Override
	protected void onHandleIntent(Intent intent) {

		while(true){
			
			try {
				Thread.sleep(180000);
				VGongyiService service = new VGongyiService(getApplicationContext());
				ArrayList<CommonEntity> list = service.getLatestCommonFeeds();
				int result = list.size();
				Log.i("msg", "result = " + result);
				CommonDAO commonDAO = new CommonDAO(getApplicationContext());
				for (int i = 0; i < list.size(); i++) {
					if (commonDAO.query(null, "id = '" + list.get(i).getId() + "'",null, null).size() > 0)
						result--;
				}
	
				if (list.size() > 0) {
					commonDAO.delete(null, null);
					for (CommonEntity entity : list)
						commonDAO.insert(entity);
				}
				Log.i("msg", "result = " + result);
				Intent mIntent = new Intent();
				mIntent.setAction("MSG_COUNT_ACTION");
				mIntent.putExtra("msg_count", result);
				getApplicationContext().sendBroadcast(mIntent);
	
			} catch (Exception e) {
				e.printStackTrace();
			}
		}
	}
}
